package comWorld.blogCatalog;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import com.gjs.tools.JDBC.JDBCTools;

import comWorld.blog.BlogInfo;
import comWorld.blog.BlogInfoManage;
import comWorld.blog.BlogInfos;

public class BlogCatalogs {
	private int userId;
	private List<BlogCatalog> catalogs;
	
	public BlogCatalogs(int userId) {
		this.userId = userId;
		this.catalogs = BlogCatalogManage.getBlogCatalogs(userId);
	}
	
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public List<BlogCatalog> getCatalogs() {
		return catalogs;
	}
	public void setCatalogs(List<BlogCatalog> catalogs) {
		this.catalogs = catalogs;
	}
	
	public BlogCatalog get(int catalogId) {
		BlogCatalog curr = null;
		for (BlogCatalog catalog : catalogs) {
			if (catalog.getCatalogId() == catalogId) {
				curr = catalog;
				break;
			}
		}
		return curr;
	}
	
	public void remove(BlogCatalog catalog) {
		catalogs.remove(catalog);
	}
	
	public void add(int index, BlogCatalog catalog) {
		catalogs.add(index, catalog);
	}
	
	public void statisticsBlogCount(int userId, int privacy) {
		BlogInfos blogInfos = BlogInfoManage.getBlogInfos(userId, privacy);
		for (BlogInfo blogInfo : blogInfos.getBlogs()) {
			BlogCatalog catalog = get(blogInfo.getCatalogId());
			int count = catalog.getBlogCount();
			catalog.setBlogCount(++count);
		}
	}
}